import React, {
  Component
} from 'react'
import { Link } from 'react-router'
import {
  WingBlank,
  WhiteSpace,
  List,
  InputItem,
  Button,
  Toast
} from 'antd-mobile'
import {
  createForm
} from 'rc-form'
import styles from './index.scss'

import request from '../../../request'

let userInfo={}

const Item = List.Item;
const Brief = Item.Brief;

class Login extends Component {
  state = {
    value: 1,
  }
  onSubmit = () => {
    let _this=this
    this.props.form.validateFields((error, value) => {
      request.post('user/login',value)
        .then(function (response) {
          if(response.data.retCode==="0000"){
            Toast.success(`${response.data.retMsg}`,1)
            userInfo="test"
            _this.context.router.push('/')
          }else{
            Toast.fail(`${response.data.retMsg}`,1)
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    });
  }
  validateAccount = (rule, value, callback) => {
    if (value && value.length > 4) {
      callback();
    } else {
      callback(new Error('帐号至少4个字符'));
    }
  }
  render() {
    const {
      getFieldProps,
      getFieldError
    } = this.props.form;
    return (
      <WingBlank size="md">
    <WhiteSpace size="lg" />
    <form>
      <List
        renderFooter={() => getFieldError('mobilePhone') && getFieldError('mobilePhone').join(',')}
      >
        <InputItem
          {...getFieldProps('mobilePhone', {
            // initialValue: '小蚂蚁',
            rules: [
              { required: true, message: '请输入手机号码' },
              { validator: this.validateAccount },
            ],
          })}
          clear
          error={!!getFieldError('mobilePhone')}
          onErrorClick={() => {
            alert(getFieldError('mobilePhone').join('、'));
          }}
          placeholder="手机号"
        >用户名</InputItem>
        <InputItem {...getFieldProps('password')} type="password">
          密码
        </InputItem>
      </List>
      <Button style={{width:"100%"}} type="primary" onClick={this.onSubmit} inline>登录</Button>
    </form>
    <div className={styles.registry}>
      <Link to='/registry'>没有账号?去注册</Link>
    </div>
  </WingBlank>
    )
  }
}

export default createForm()(Login);